import requests,os
import json

url = "https://fjhmtd.com:15354/glaf/mx/form/data/gridData"
headers = {
    "Accept": "application/json, text/javascript, */*; q=0.01",
    "Accept-Encoding": "gzip, deflate, br, zstd",
    "Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6",
    "Cache-Control": "no-cache",
    "Connection": "keep-alive",
    "Content-Type": "application/json",
    "Cookie": "SECKEY_ABVK=066VjZa+eQlzmUlMNf3+/81S6/RTY2lRO03+OGB+89o%3D; BMAP_SECKEY=066VjZa-eQlzmUlMNf3-_5EPfBF_sAxGhBH90J87rBpeEPNjAJ-eX592XJc5py1jxrqXxjLmSrNZNmGyO_3yMsH5X-CZ-2EFPGr1bwTA6rBwgQXpawZ3SHlFPNrUK4dtRugFlciFzRxunP0fprwqdCN6mF602n9jyMRA_r4Nonk8uo_jbVUJDH2E-qjqlpx2; JSESSIONID=aaafvLYQVHFJN9B3Cc0gz; GLAF_COOKIE=99B9324E43A8CD2CCF7B84AB3756982FBC6887851D0475E80555F8DBEC0E81F1A4125B8D14E54084F2A43CCDDBA92EC3F7C0ACBBC9E6FD0248A11FCBA0B43403223DA4DD98110A10A9444FE6746DAAF7E57C79C620A256E58987F19E4FBAFB109595047D98CE67CA05BFAC62340E91ED476964B4EF89B98E706FB35E608A3D0F4BBCA2EE8372AFAD052752B4079BA533AF5AAFCC362358ABA9A0E3ED71D9C13162B2D13FF03BFE30C29ACCB9DE4AD55E0C9E3BC552E87827551DFE4111B404B0B0A26C52C4717BF6B5635FDFF582562A10485B21996EBAC57D3486E83A28200F5C2A6A0A2E0CD462CBCC363F8ECFA5B4E5F356FCDD4231814D3EDFB201E713B2; GLAF_JWT_COOKIE=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VySWQiOiJ5c3J5Iiwic3lzdGVtTmFtZSI6ImRlZmF1bHQiLCJMT0dJTl9JUCI6IjIyMC4xOTEuMjMwLjEzMSIsIkdMQUZfTE9HSU5fQUNUT1JJRCI6InlzcnkiLCJUUyI6IjE3MjUzMzI3NzAyMjQiLCJ0b2tlbiI6IjE2NzQyYmI2OTM2N2Y0MzU0YTZiYTk3N2NmNWQxM2MwMzBjMDkzMDYiLCJleHAiOjQzMTczMzI3NzAsIm5iZiI6MTcyNTMzMjc3MH0.0a2RG8DduDkOfwoKzaEjuzTu9AYNKJPqXdkYoGv6pDo",
    "Host": "fjhmtd.com:15354",
    "Origin": "https://fjhmtd.com:15354",
    "Pragma": "no-cache",
    "Referer": "https://fjhmtd.com:15354/glaf/mx/form/page/viewPage?id=b21b2052d2844269bccf6fc708c0b323&cockpit_parameters1=&cockpit_parameters2=&cockpit_parameters3=&cockpit_parameters4=&cockpit_parameters5=&person_id=&number=&xm_id=20230517/cjgly-0000001_GCXQMBa8&project_id=20230517/cjgly-0000001_GCXQMBa8",
    "Sec-Fetch-Dest": "empty",
    "Sec-Fetch-Mode": "cors",
    "Sec-Fetch-Site": "same-origin",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36 Edg/124.0.0.0",
    "X-Requested-With": "XMLHttpRequest",
    "sec-ch-ua": "\"Chromium\";v=\"124\", \"Microsoft Edge\";v=\"124\", \"Not-A.Brand\";v=\"99\"",
    "sec-ch-ua-mobile": "?0",
    "sec-ch-ua-platform": "\"Windows\""
}

payload = {
    "rid": "5631130436fd439cb8180d2db14e665b",
    "parentId": "",
    "params": json.dumps({
        "col1705993253862": "",
        "col1705993253861": "",
        "col1705993253860": None,
        "col1705993253859": None,
        "col1705993253858": "",
        "col1706325086065": "",
        "page": 1,
        "pageSize": 30
    }),
    "col1705993253862": "",
    "col1705993253861": "",
    "col1705993253860": None,
    "col1705993253859": None,
    "col1705993253858": "",
    "col1706325086065": "",
    "page": 1,
    "pageSize": 30,
    "rows": 30,
    "waitSortValue": None,
    "sort": [],
    "sortNames": ""
}

# 发送请求
response = requests.post(url, headers=headers, json=payload)

# 指定下载目录
download_dir = "downloads"
if not os.path.exists(download_dir):
    os.makedirs(download_dir)

# 检查请求是否成功
if response.status_code == 200:
    data = response.json().get("data")
    files_info = []
    base_url = "https://fjhmtd.com:15354/glaf"

    for i in data:
        info = {}
        file_name = i['t5_0_c4']  # 假设 't5_0_c3' 包含文件名
        file_url = base_url + i['t5_0_c3']  # 完整URL
        print(file_url)

        # 下载文件
        file_path = os.path.join(download_dir, file_name)
        try:
            file_response = requests.get(file_url, stream=True)
            if file_response.status_code == 200:
                with open(file_path, 'wb') as f:
                    for chunk in file_response.iter_content(chunk_size=1024):
                        if chunk:
                            f.write(chunk)
                print(f"文件下载成功: {file_name}")
            else:
                print(f"下载失败，状态码: {file_response.status_code}, 文件: {file_name}")
        except Exception as e:
            print(f"下载过程中出错: {e}, 文件: {file_name}")

        # 保存文件信息
        info['name'] = file_name
        info['url'] = file_url
        info['path'] = file_path
        files_info.append(info)
else:
    print(f"请求失败，状态码: {response.status_code}")
